import subprocess
import sys

prc=subprocess.run("mode2 -d /dev/lirc0 -H default>./current.ctrl",shell=True)



import os
from time import sleep



def recording_code(content:str):
    ret:str=""
    li:list= content.split("\n")
    for i in li:
        if i.startswith("s"):
            ret+="s"
        else:
            ret+="p"
        ret+=i.split(" ")[1][0:-3]
    return ret
    

prc1=subprocess.Popen(['stat' ,'./current.ctrl'] ,stdout=subprocess.PIPE)
r1=prc1.stdout.read()


while True:
    sleep(1)
    prc2=subprocess.Popen(['stat' ,'./current.ctrl'] ,stdout=subprocess.PIPE)
    r2=prc2.stdout.read()
    if r1!=r2:
        file= open("./current.ctrl",mode="r")
        rc:str=recording_code(file.read())
        print(rc)
        print(r1)
        print(r2)
        file.close()
        file= open("./current.ctrl",mode="w")
        file.write("")
        file.close()
        prc3=subprocess.Popen(['stat' ,'./current.ctrl'] ,stdout=subprocess.PIPE)
        r3=prc3.stdout.read()
        r1=r3
    
